﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Net;


/// <summary>
/// Summary description for ClassSendMail
/// </summary>
public class ClassSendMail
{
	public ClassSendMail()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public string Send_Email(string SendFrom,string SendTo, string Subject, string Body)
       { 
           try
           {
               System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");


               bool result = regex.IsMatch(SendTo);
               if (result == false)
               {
                   return "Địa chỉ email không hợp lệ.";
               }
               else
               {
                   System.Net.Mail.SmtpClient smtp = new SmtpClient();
                   System.Net.Mail.MailMessage msg = new MailMessage(SendFrom,SendTo,Subject,Body);
                   msg.IsBodyHtml = true;
                   smtp.Host = "smtp.gmail.com";//Sử dụng SMTP của gmail
                   NetworkCredential mailAuthentication = new NetworkCredential("khoi.nguyenhongmanh@gmail.com", "drmcompany");
                   smtp.EnableSsl = true;
                   smtp.UseDefaultCredentials = false;
                   smtp.Credentials = mailAuthentication;
                   smtp.Send(msg);
                   return "Email đã được gửi đến: " + SendTo + ".";
               }
           }
           catch(Exception ex)
           {
               return ex.Message;
           }
       } 
    public string Send_Email_With_Attachment(string SendTo, string SendFrom, string Subject, string Body, string AttachmentPath)
       {
           try
           {
               System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
               string from = SendFrom;
               string to = SendTo;
               string subject = Subject;
               string body = Body;
               bool result = regex.IsMatch(to);
               if (result == false)
               {
                   return "Địa chỉ email không hợp lệ.";
               }
               else
               {
                   try
                   {
                       MailMessage em = new MailMessage(from, to,subject, body);
                       Attachment attach = new Attachment(AttachmentPath);
                       em.Attachments.Add(attach);
                       em.Bcc.Add(from); 
                       System.Net.Mail.SmtpClient smtp = new SmtpClient();
                       smtp.Host = "smtp.gmail.com";//Ví dụ xử dụng SMTP của gmail                    
                       smtp.Send(em); 
                       return "";
                   }
                   catch (Exception ex)
                   {
                       return ex.Message;
                   }
               }
           }
           catch (Exception ex)
           {
               return ex.Message;
           }
       } 
}